#!/usr/bin/env python

import sys
import math

def remove_outliers(values, x=5.0):
    """Removes the top and bottom x percent"""
    remove_this_many = int(math.floor(len(values)/100.0*x))
    if remove_this_many > 0:
        values.sort()
        return values[remove_this_many:-remove_this_many]
    else:
        return values

def avg(values):
    """Calculates the average of a sequence of values."""
    return sum(values)/len(values)

def stddev(values):
    """Calculates the standard deviation of a sequence of values."""
    return math.sqrt(sum(map(lambda x: x**2, values))/len(values) - avg(values)**2)

def filename_to_order(filename):
    if 'tiny' in filename:
        return 0
    elif 'small' in filename:
        return 1
    elif 'medium' in filename:
        return 2
    else:
        return 3

def filename_to_label(filename):
    if 'tiny' in filename:
        return 'Tiny'
    elif 'small' in filename:
        return 'Small'
    elif 'medium' in filename:
        return 'Medium'
    else:
        return 'Large'

def compare_filenames(file1, file2):
    return cmp(filename_to_order(file1), filename_to_order(file2))

# Check command line args.
if len(sys.argv) < 2:
    print 'Usage: munch.py file_1 [file_2 ... file_n]'
    sys.exit(1)

# Start reading in the files.
data = {}
for filename in sys.argv[1:]:
    data[filename] = {}
    with open(filename) as infile:
        lines = infile.readlines()
        
    for line in lines:
        try:
            key, value = line.split(' ')
        except:
            continue
        if not data[filename].has_key(key):
            data[filename][key] = []
        data[filename][key].append(float(value))

# Do some data handling.
for filename in data.keys():
    for key, value in data[filename].items():
        length = len(value)
        value = remove_outliers(value)
        data[filename][key] = (length, avg(value), stddev(value))

# Print the results.
output = data.keys()
output.sort(compare_filenames)
for filename in output:
    header = True
    for key, value in data[filename].items():
        print filename_to_label(filename) if header else '-', key, value[0], value[1], value[2]
        header = False

